#include "postgresd.h"

#include <QObject>
#include <QTabWidget>
#include <QLineEdit>
#include <QSpinBox>
#include <QGroupBox>
#include <QFormLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QDebug>

#include "../../core/core.h"
#include "../../core/settingssql.h"

class PostgreSD::PostgreSDPrivate {
public:
	PostgreSDPrivate();
	~PostgreSDPrivate();

	QTabWidget *m_pTab;


	QLineEdit *m_pHostName;
	QLineEdit *m_pUserName;
	QLineEdit *m_pPassword;
	QSpinBox *m_pPort;
	QCheckBox *m_pSSL;

	QLineEdit *m_pDBName;
	QLineEdit *m_pPrefix;


	bool m_spinboxChanged;
	bool m_checkboxChanged;
};

PostgreSD::PostgreSDPrivate::PostgreSDPrivate(){
	m_pTab = NULL;

	m_spinboxChanged = false;
	m_checkboxChanged = false;
}

PostgreSD::PostgreSDPrivate::~PostgreSDPrivate(){
	delete m_pTab;
}



//end of d-class part
//------------------------------------------------------------------------------
PostgreSD::PostgreSD() :
	d(new PostgreSDPrivate())
{

}

PostgreSD::~PostgreSD(){
	delete d;
	Q_CLEANUP_RESOURCE(postgreres);
}

QString PostgreSD::category() const {
	return QObject::tr("PostgreSQL");
}

QIcon PostgreSD::categoryIcon() const {
	Q_INIT_RESOURCE(postgreres);
	return QIcon(":/settings/res/postgresql32.png");
}

QTabWidget* PostgreSD::createPages(){
	if( d->m_pTab )
		return d->m_pTab;



	d->m_pTab = new QTabWidget();


	QWidget *general = new QWidget( d->m_pTab );
	d->m_pTab->addTab( general, QObject::tr("Główne") );

	//Serwer
	//---------
	QGroupBox *group1 = new QGroupBox( QObject::tr("Serwer"), general );
	QFormLayout *layout1 = new QFormLayout( group1 );

	d->m_pHostName = new QLineEdit( group1 );
	d->m_pHostName->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/hostname",
												 QVariant("localhost") )
			.toString() );
	d->m_pHostName->setToolTip( QObject::tr("To pole nie może być puste\n"
											"Jeśli tak się stanie, zostanie "
											"przywrócona ostatnia wartość") );

	d->m_pUserName = new QLineEdit( group1 );
	d->m_pUserName->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/username",
												 QVariant("postgres") )
			.toString() );
	d->m_pUserName->setToolTip( QObject::tr("To pole nie może być puste\n"
											"Jeśli tak się stanie, zostanie "
											"przywrócona ostatnia wartość") );

	d->m_pPassword = new QLineEdit( group1 );
	d->m_pPassword->setEchoMode( QLineEdit::Password);
	d->m_pPassword->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/password",
												 QVariant() )
			.toString() );

	d->m_pPort = new QSpinBox( group1 );
	d->m_pPort->setRange(0, 65535);
	d->m_pPort->setValue(
			Core::instance()->settings()->value( "plugin/PostgreSQL/port",
												 QVariant(5432) )
			.toInt() );
	d->m_pPort->setToolTip( QObject::tr("Domyślny port: 5432") );
	connect( d->m_pPort, SIGNAL(valueChanged(int)),
			 this, SLOT(spinBoxChanged(int)) );


	d->m_pSSL = new QCheckBox( QObject::tr("Obecne"), group1 );
	d->m_pSSL->setChecked(
			Core::instance()->settings()->value( "plugin/PostgreSQL/ssl",
												 QVariant(false) )
			.toBool() );
	d->m_pSSL->setToolTip(
			QObject::tr("Określa czy ma być nawiązanie połączenie szyfrowane "
						"o ile serwer je wspiera,\nw przeciwnym wypadku "
						"nastąpi próba nawiązania połączenia nieszyfrowanego"));
	connect( d->m_pSSL, SIGNAL(stateChanged(int)),
			 this, SLOT(checkBoxChanged(int)) );

	layout1->addRow( QObject::tr("Domena/Serwer:"), d->m_pHostName );
	layout1->addRow( QObject::tr("Login:"), d->m_pUserName );
	layout1->addRow( QObject::tr("Hasło:"), d->m_pPassword );
	layout1->addRow( QObject::tr("Port:"), d->m_pPort );
	layout1->addRow( QObject::tr("SSL:"), d->m_pSSL );

	group1->setLayout( layout1 );


	//baza
	//---------
	QGroupBox *group2 = new QGroupBox( QObject::tr("Baza"), general );
	QFormLayout *layout2 = new QFormLayout( group2 );

	d->m_pDBName = new QLineEdit( group2 );
	d->m_pDBName->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/database",
												 QVariant("beaver") )
			.toString() );
	d->m_pDBName->setToolTip( QObject::tr("To pole nie może być puste\n"
										  "Jeśli tak się stanie, zostanie "
										  "przywrócona ostatnia wartość") );

	d->m_pPrefix = new QLineEdit( group2 );
	d->m_pPrefix->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/prefix",
												 QVariant("beaver_") )
			.toString() );


	layout2->addRow(QObject::tr("Nazwa bazy:"), d->m_pDBName);
	layout2->addRow(QObject::tr("Prefix:"), d->m_pPrefix);


	group2->setLayout( layout2 );


	general->setLayout( new QVBoxLayout(general) );
	general->layout()->addWidget(group1);
	general->layout()->addWidget(group2);


	return d->m_pTab;
}

void PostgreSD::save(){
	if( d->m_pHostName->isModified() ){
		if( !d->m_pHostName->text().isEmpty() ){
			Core::instance()->settings()->setValue("plugin/PostgreSQL/hostname",
													d->m_pHostName->text() );
		}else{
			d->m_pHostName->setText(
					Core::instance()->settings()->value(
							"plugin/PostgreSQL/hostname",
							QVariant("localhost") ).toString() );
		}
	}


	if( d->m_pUserName->isModified() ){
		if( !d->m_pUserName->text().isEmpty() ){
			Core::instance()->settings()->setValue("plugin/PostgreSQL/username",
												   d->m_pUserName->text() );
		}else{
			d->m_pUserName->setText(
					Core::instance()->settings()->value(
							"plugin/PostgreSQL/username",
							QVariant("postgres") ).toString() );
		}
	}

	if( d->m_pPassword->isModified() ){
		Core::instance()->settings()->setValue( "plugin/PostgreSQL/password",
												d->m_pPassword->text() );
	}

	if( d->m_spinboxChanged )
		Core::instance()->settings()->setValue( "plugin/PostgreSQL/port",
												d->m_pPort->value() );

	if( d->m_checkboxChanged )
		Core::instance()->settings()->setValue( "plugin/PostgreSQL/ssl",
												d->m_pSSL->isChecked() );


	if( d->m_pDBName->isModified() ){
		if( !d->m_pDBName->text().isEmpty() ){
			Core::instance()->settings()->setValue("plugin/PostgreSQL/database",
												   d->m_pDBName->text() );
		}else{
			d->m_pDBName->setText(
					Core::instance()->settings()->value(
							"plugin/PostgreSQL/database",
							QVariant("beaver") ).toString() );
		}
	}

	if( d->m_pPrefix->isModified() ){
		Core::instance()->settings()->setValue( "plugin/PostgreSQL/prefix",
												d->m_pPrefix->text() );
	}


	d->m_checkboxChanged = false;
	d->m_spinboxChanged = false;

	d->m_pPrefix->setModified( false );
	d->m_pDBName->setModified( false );
	d->m_pPassword->setModified( false );
	d->m_pHostName->setModified( false );
}

void PostgreSD::reset(){
	d->m_pHostName->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/hostname",
												 QVariant("localhost") )
			.toString() );


	d->m_pPassword->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/password",
												 QVariant() )
			.toString() );


	d->m_pPort->setValue(
			Core::instance()->settings()->value( "plugin/PostgreSQL/port",
												 QVariant(433) )
			.toInt() );

	d->m_pSSL->setChecked(
			Core::instance()->settings()->value( "plugin/PostgreSQL/ssl",
												 QVariant(false) )
			.toBool() );

	d->m_pDBName->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/database",
												 QVariant("beaver") )
			.toString() );

	d->m_pPrefix->setText(
			Core::instance()->settings()->value( "plugin/PostgreSQL/prefix",
												 QVariant("beaver_") )
			.toString() );

	d->m_checkboxChanged = false;
	d->m_spinboxChanged = false;

	d->m_pPrefix->setModified( false );
	d->m_pDBName->setModified( false );
	d->m_pPassword->setModified( false );
	d->m_pHostName->setModified( false );
}

void PostgreSD::spinBoxChanged(int){
	d->m_spinboxChanged = true;
}

void PostgreSD::checkBoxChanged(int){
	d->m_checkboxChanged = true;
}
